// pages/question/index.js
import { IndexModel } from "../../models/index.js";
const indexModel = new IndexModel();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        topicList:null,
        titleMsg:"",
        topicCount:0,
        currentIndex:0,
        page:1,
        courseId:0,
        mockId:0,
        toastShow:false,
        userAnswer:null,
        isShowDialog:false,//继续答题弹窗
        returnShowDialog:false,//退出弹窗
        examTime:0,//考试时间
        timeStatus:false,//是否开始倒计时
        remainingTime:0,//剩余时间
        isSubmitBack:0,//1直接交卷选择的题号返回过来的
    },
    nextQuestion(event){
        let userAnswer=event.detail.isAnswer;
        let clickPage=event.detail.clickPage;
        let isAddOne=0;
        if(clickPage==undefined){
            clickPage=this.data.page;
            isAddOne=1;
        }
        clickPage=parseInt(clickPage);
        if(!userAnswer){
            userAnswer=null;
        }
        if(event.detail.indexNum>=this.data.topicCount){
            wx.showToast({
                title: "考完了",
                icon: 'none',
                duration: 2000
              })
        }else{
            if(event.detail.topicIndex>=this.data.topicList.length-1||clickPage!=this.data.page){
                let topIndex=event.detail.topicIndex+1;
                if(isAddOne==1){
                    clickPage=clickPage+1;
                    topIndex=0;
                }
                this.loadData(this.data.courseId,this.data.mockId,clickPage,1,topIndex);
            }else{
                this.setData({
                    currentIndex:event.detail.topicIndex+1
                })
                if(this.data.topicList[this.data.currentIndex].user_answer_record){
                    userAnswer=this.data.topicList[this.data.currentIndex].user_answer_record;
                }
            }
            
            this.setData({
                userAnswer:userAnswer
            });
        }
       
    },
    /**
     * 生命周期函数--监听页面加载
     */
    async onLoad(options) {
        let courseId=options.courseId;
        let mockId=options.mockId;
        let isLoad=options.isLoad;
        let progressPage=options.progressPage;
        if(isLoad==2){
            this.setData({
                isShowDialog:true,
                page:progressPage,
                courseId:courseId,
                mockId:mockId
            });
        }else{
            this.loadData(courseId,mockId,1,isLoad,0);
        }
    },
    //取消继续答题
    cancelMock(){
        //放弃本次考试重新答题了；重新出一份重头开始
        this.loadData(this.data.courseId,-1,1,1,0);
    },
    //继续上次答题
    continueMock(){
        //加载上次答题所在的那一页数据
        this.loadData(this.data.courseId,this.data.mockId,this.data.page,2,0);
    },
   
    async loadData(courseId,mockId,page,isLoad,index) {
        const data=await indexModel.mockExamQuestions(courseId,mockId,page);
        const newpage=(data.topics.length>0?data.page:this.data.page);
        let currentIndex=index;
        if(isLoad==2){
            //从上次答题记录过来的
            if(data.progress==0){
                currentIndex=0;
            }else{
                currentIndex=data.progress%10-1;
            }
        }
        this.setData({
            topicList:data.topics,
            titleMsg:data.titleMsg,
            topicCount:data.topicCount,
            courseId:courseId,
            mockId:data.mockExamId,
            page:newpage,
            currentIndex:currentIndex,
            examTime:data.examTime,
            timeStatus:true
          });
      },

      //点击左上角返回
      returnBtnClick(){
          this.setData({
            returnShowDialog:true
          });
      },
      //重置答题
      resetMock(){
        this.loadData(this.data.courseId,-1,1,1,0);
        this.setData({
            returnShowDialog:false
        });
      },
      //继续答题
      returnMock(){
        this.setData({
            returnShowDialog:false
        });
      },
      //暂时退出
      exitMock(){
        //需要记录本次用的时间
        indexModel.examTimeIngSave(this.data.remainingTime,this.data.courseId,this.data.mockId);
        wx.switchTab({
            url: `/pages/index/index`,
        });
      },
       //交卷
    submitExam(datatype){
        let submitType=0;
        if(datatype==1){
            submitType=1;
        }
        indexModel.examTimeIngSave(this.data.remainingTime,this.data.courseId,this.data.mockId);
        wx.navigateTo({
            url: `/pages/submit_exams/index?submitType=${submitType}&courseId=${this.data.courseId}&mockId=${this.data.mockId}&courseName=${this.data.titleMsg}`
        });
    },
      //记录当前倒计时剩余的时间
      examTimeing(e){
          this.setData({
            remainingTime:e.detail.remainingTime
          });
          if(e.detail.remainingTime<=0){
              //没时间了，直接交卷
              this.submitExam(1);
          }
      },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        if(this.data.isSubmitBack==1){
            this.loadData(this.data.courseId,this.data.mockId,this.data.page,1,this.data.currentIndex);
        }
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {
        const app = getApp();
        if(!app.globalData.mockUnload){
            this.exitMock();
        }
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})